Ext.define('cloud.controller.Area', {
    extend: 'Ext.app.Controller',
    config: {
        refs: {
            area: 'area',
            areaadd: 'areaadd',
            areaedit: 'areaedit',
            area_add: 'area #area_add',
            province: 'areaadd #province',
            city: 'areaadd #city',
            area_submit: 'areaadd #area_submit',
            area_edit_submit: 'areaedit #area_edit_submit',
            select: 'area #area_select'
        },
        control: {
            /**
             * show areaadd page
             */
            area_add: {
                tap: function () {
                    Ext.Viewport.mask({xtype: "loadmask"});
                    var areaadd = this.getAreaadd()|| Ext.create('cloud.view.area.AreaAdd');
                    this.getArea().push(areaadd)
                    this.getArea_add().enable();
                    Ext.Viewport.unmask();
                }
            },
            /**
             * show areaedit page
             */
            select: {
                itemtap: function (list, item, index, record) {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var appinfostore = Ext.getStore('AppInfoGetStore');
                    var areaedit = this.getAreaedit()||Ext.create('cloud.view.area.AreaEdit');
                    areaedit.setRecord(record);
                    Ext.Viewport.unmask();
                    this.getArea().push(areaedit);

                }
            },
            /**
             * add area
             */
            area_submit: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var province = Ext.getCmp('province').getValue(),
                        city = Ext.getCmp('city').getValue(),
                        status = Ext.getCmp('status').getValue(),
                        caid = localStorage.getItem('caid'),
                        starttime = Ext.getCmp('starttime').getValue(),
                        endtime = Ext.getCmp('endtime').getValue();
                    if (starttime >= endtime) {
                        Ext.Viewport.unmask();
                        return Ext.Msg.alert('结束时间必须大于开始时间');
                    }
                    var areamodel = Ext.create('cloud.model.AppDistrictModel', {id: city, status: status, caid: caid, starttime: starttime, endtime: endtime});
                    var errors = areamodel.validate();

                    if (errors.isValid()) {
                        this.getAreaadd().submit({
                            url: GLOBAL.api_url + 'area_add&caid=' + caid,
                            scope: this,
                            method: 'POST',
                            success: this.executeSuccess
                        });

                    } else {
                        var message = "";
                        Ext.each(errors.items, function (rec) {
                            message += rec.getMessage() + "<br>";
                        });
                        Ext.Msg.alert("验证失败", message);
                    }
                    Ext.Viewport.unmask();
                }
            },
            /**
             * edit area
             */
            area_edit_submit: {

                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    starttime = Ext.getCmp('starttime').getValue(),
                        endtime = Ext.getCmp('endtime').getValue();
                    if (starttime >= endtime) {
                        Ext.Viewport.unmask();
                        return Ext.Msg.alert('结束时间必须大于开始时间');
                    }

                    this.getAreaedit().submit({
                        url: GLOBAL.api_url + 'area_update',
                        scope: this,
                        method: 'POST',
                        success: this.executeSuccess
                    });
                    Ext.Viewport.unmask();
                }
            }
        },
        pagePicker: undefined,
        id: undefined
    },
    executeSuccess: function (from, result) {
        if (result.Variables.Result.code == 0) {
            Ext.getStore('AreaAllFindStore').load({params: {caid: localStorage.getItem('caid')}});
            this.getArea().pop();
        } else {
            Ext.Msg.alert(result.Variables.Message.messagestr);
        }
    }
});